package by.dkusch.aircompany.model.tables;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import by.dkusch.aircompany.model.tables.base.BaseEntity;
import by.dkusch.aircompany.model.tables.enums.Gender;
import by.dkusch.aircompany.model.tables.enums.PersonPosition;

@Entity
public class Person extends BaseEntity<Long> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@NotNull
	@Size(max = 50)
	@Column
	private String tabCode;

	@NotNull
	@Size(max = 50)
	@Column
	private String firstName;

	@NotNull
	@Size(max = 50)
	@Column
	private String secondName;

	@Size(max = 50)
	@Column
	private String lastName;

	@Column
	@Enumerated(EnumType.ORDINAL)
	@NotNull
	private PersonPosition posId;


	@Column
	@Enumerated(EnumType.ORDINAL)
	@NotNull
	private Gender gender;

	@Column(name = "date_of_b")
	private Date dateOfB;

	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Crew.class)
	@JoinTable(name = "crew_person", joinColumns = { @JoinColumn(name = "person_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "crew_id", nullable = false, updatable = false) })
	private Set<Crew> crewSet;

	@Column(name = "u_FIO")
	private String uFIO;

	public Set<Crew> getCrewSet() {
		return crewSet;
	}

	public void setCrewSet(Set<Crew> crewSet) {
		this.crewSet = crewSet;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSecondName() {
		return secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public PersonPosition getPosId() {
		return posId;
	}

	public void setPosId(PersonPosition posId) {
		this.posId = posId;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public Date getDateOfB() {
		return dateOfB;
	}

	public void setDateOfB(Date dateOfB) {
		this.dateOfB = dateOfB;
	}

	public String getTabCode() {
		return tabCode;
	}

	public void setTabCode(String tabCode) {
		this.tabCode = tabCode;
	}

	public String getuFIO() {
		if (uFIO == null) {
			setuFIO();
		}
		return uFIO;
	}

	public void setuFIO() {
		StringBuilder sb = new StringBuilder();
		sb.append(firstName);
		sb.append(' ');
		sb.append(secondName);
		if (lastName != null) {
			sb.append(' ');
			sb.append(lastName);
		}
		uFIO = sb.toString().toUpperCase();
	}


}
